package com.iwe.crm.workbench.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iwe.crm.base.bean.ResultVo;
import com.iwe.crm.base.exception.CrmException;
import com.iwe.crm.base.util.CommonUtil;
import com.iwe.crm.user.bean.User;
import com.iwe.crm.user.service.UserService;
import com.iwe.crm.workbench.bean.Activity;
import com.iwe.crm.workbench.bean.ActivityRemark;
import com.iwe.crm.workbench.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private UserService userService;

    @GetMapping("/queryList")
    public PageInfo<Activity> queryList(int page,int pageSize,Activity activity){
        List<Activity> activities = activityService.queryList(page,pageSize,activity);
        PageInfo<Activity> pageInfo = new PageInfo<>(activities);
        return pageInfo;
    }

    //查询所有者数据
    @GetMapping("/queryAllUsers")
    public List<User> queryAllUsers(){
        return userService.queryAllUsers();
    }

    //添加或修改 添加:主键为null 修改:有主键
    @PostMapping("/saveOrUpdate")
    public ResultVo saveOrUpdate(Activity activity, HttpSession session){
        ResultVo resultVo = null;
        try{
            //获取当前登录用户
            User currentUser = CommonUtil.getCurrentUser(session);
            resultVo = activityService.saveOrUpdate(activity,currentUser);
        }catch (CrmException e){
            resultVo.setMessage(e.getMessage());
        }
        return resultVo;
    }

    //根据主键查询市场活动
    @GetMapping("queryById")
    public Activity queryById(String id){
        return activityService.queryById(id);
    }


    //跳转到市场活动详情页
    @GetMapping("/toDetail")
    public Activity toDetail(String id){
        return activityService.toDetail(id);
    }
    //批量删除
    @GetMapping("/deleteBatch")
    public ResultVo deleteBatch(String ids){
        ResultVo resultVo = new ResultVo();
        try{
            activityService.deleteBatch(ids);
            resultVo.setOk(true);
            resultVo.setMessage("删除数据成功");
        }catch (CrmException e){

        }
        return resultVo;
    }

    //保存备注信息
    @GetMapping("/saveRemark")
    public ResultVo saveRemark(ActivityRemark activityRemark,HttpSession session){
        ResultVo resultVo = new ResultVo();
        try{
            User user = CommonUtil.getCurrentUser(session);
            activityService.saveRemark(activityRemark,user);
            resultVo.setOk(true);
            resultVo.setMessage("添加备注成功");
        }catch (CrmException e){

        }
        return resultVo;
    }

    //删除备注
    @GetMapping("/deleteRemark")
    public ResultVo deleteRemark(String id){
        ResultVo resultVo = new ResultVo();
        try{
            activityService.deleteRemark(id);
            resultVo.setOk(true);
            resultVo.setMessage("删除备注成功");
        }catch (CrmException e){

        }
        return resultVo;
    }

    //修改备注
    @GetMapping("/updateRemark")
    public ResultVo updateRemark(ActivityRemark activityRemark,HttpSession session){
        ResultVo resultVo = new ResultVo();
        try{
            User user = CommonUtil.getCurrentUser(session);
            activityService.updateRemark(activityRemark,user);
            resultVo.setOk(true);
            resultVo.setMessage("修改备注成功");
        }catch (CrmException e){

        }
        return resultVo;
    }


}
